package com.hos.hospital.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.hos.hospital.entity.Admin;
import com.hos.hospital.entity.Doctor;
import com.hos.hospital.entity.Section;
import com.hos.hospital.service.AdminService;
import com.hos.hospital.service.DoctorService;
import com.hos.hospital.service.SectionService;
import com.hos.hospital.service.impl.AdminServiceImp;
import com.hos.hospital.service.impl.DoctorServiceImp;
import com.hos.hospital.utils.ExcelUtils;
import com.hos.hospital.utils.StringRandom;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 登录控制层
 */
@Controller
@RequestMapping("/login")
public class LoginController {


	     @Autowired
	     private AdminServiceImp as;
	     
	     @Autowired
	     private DoctorServiceImp ds;
	     
	     
	     @Value("${fileUrl}") 
	     private String filePath;//在配置文件中获取文件的保存路径
	     
	     
		/**
		      * 后台登陆界面
		 * @throws IOException 
		 */
		@RequestMapping("/afterView")
		public String afterLogin(Integer type,Model model) {
		    if(type == null) {
		        type = 1;
		    }
		    List<Admin> findAll = as.findAll();
		    System.out.println("jpa数据长度:"+findAll.size());
		    findAll.forEach((list) -> {
		    	  System.out.println("数据:"+list.getUsername()); 
		    });
		    model.addAttribute("type",type);
		    return "login";
		}
	
	    
		 /**
		        * 后台登陆界面
		  */
		 @RequestMapping("/index")
	     public String index(Integer type,Model model) {
		       if(type == null){
		           type = 1;
		       }
		       model.addAttribute("type",type);
		       return "login";
		 }

		   /**
		     * 登陆验证
		     * @return
		     */
		    @RequestMapping("/verificatio")
		    public String verificatio(String username, String password, Integer type, HttpServletRequest request,Model model) {
		        HttpSession session = request.getSession();
		        session.setAttribute("type",type);
		        //类型为1是医院 2是管理员
		         if(type == 1){
		        	 Doctor doctor = new  Doctor();
		        	 doctor.setUsername(username);
		        	 doctor.setPasswoed(password);
		        	 List<Doctor> doctorlist = ds.findAll(doctor);
		             if(doctorlist.size() <= 0){
		                 model.addAttribute("message","密码错误");
		                 model.addAttribute("type",type);
		                 return "login";
		             }
		             session.setAttribute("DOCTOR",doctorlist.get(0));
		             return "redirect:/doctor/index";
		         }
		        Admin admin = new Admin();
		        admin.setUsername(username);
		        admin.setPassword(password);
		        List<Admin> adminlist  = as.findAll(admin);
		        if(adminlist.size() <= 0){
		            model.addAttribute("message","密码错误");
		            model.addAttribute("type",type);
		            return "login";
		        }
		        session.setAttribute("ADMIN",adminlist.get(0));
		        return "redirect:/admin/doctorList";
		    }
		   
		    /**
		     * 退出登录
		     * @param request
		     * @return
		     */
		    @RequestMapping("/sessionInvalidate")
		    public String boot(HttpServletRequest request,Model model) {
		        HttpSession session = request.getSession();
		        Integer type = (Integer) session.getAttribute("type");
		        if(type == null){
		            type=1;
		        }
		        model.addAttribute("type",type);
		        session.invalidate();   //session销毁
		        return "login";
		    }
		   
		 

}
